//
//  LJFRegisterController.m
//  SevenZhou
//
//  Created by qianfeng on 15/9/26.
//  Copyright (c) 2015年 ljf. All rights reserved.
//

#import "LJFRegisterController.h"
#import "LJFHttpManager.h"
#import "Connector.h"

@interface LJFRegisterController ()<UITextFieldDelegate>

@property (weak, nonatomic) IBOutlet UITextField *nameText;

@property (weak, nonatomic) IBOutlet UITextField *firstPassword;

@property (weak, nonatomic) IBOutlet UITextField *secondPassword;

@property (weak, nonatomic) IBOutlet UITextField *emial;

@property (weak, nonatomic) IBOutlet UIButton *registerButton;

@end

@implementation LJFRegisterController



- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self addTextNotification];
}

- (void)addTextNotification
{
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textChange) name:UITextFieldTextDidChangeNotification object:nil];
}

- (void)textChange
{
    if (_nameText.text.length>0 && _firstPassword.text.length>0 && _secondPassword.text.length>0 && _emial.text.length>0)
    {
        _registerButton.alpha = 1.0;
        _registerButton.enabled = YES;
    }
    else
    {
        _registerButton.alpha = 0.5;
        _registerButton.enabled = NO;
    }
}

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (IBAction)myRegister:(id)sender
{
    if (![_firstPassword.text isEqualToString:_secondPassword.text])
    {
        UIAlertView *alter = [[UIAlertView alloc] initWithTitle:@"提示" message:@"两次输入的密码不一样,请重新输入" delegate: self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alter show];
        
        return;
    }
    
    LJFHttpManager *manager = [LJFHttpManager shareInstance];
    
    NSString *urlStr = [NSString stringWithFormat:URL_REGISTER_parmater ,_emial.text,_nameText.text,_firstPassword.text];
    
    [manager getDataFromNetWithUrl:URL_REGISTER AndParameterStr:urlStr success:^(id responseObj)
    {
        NSLog(@"dict == %@",responseObj);
        
        NSNumber *num = responseObj[@"status"][@"succeed"];
        
        int succeed = [num intValue];
        
        if (succeed == 0)
        {
            UIAlertView *alter = [[UIAlertView alloc] initWithTitle:@"提示" message:responseObj[@"status"][@"error_desc"] delegate: self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
            [alter show];
            
        }else if (succeed == 1)
        {
            NSLog(@"dict == %@",responseObj);
            
            [self back:nil];
        }

        
    } failure:^(NSError *error)
    {
        NSLog(@"error == %@",error);
    }];
}

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [self.view endEditing:YES];
}

#pragma mark -UITextFieldDelegate

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    
    return YES;
}

//- (void)successBack
//{
//    [_alter removeFromSuperview];
//    _alter = nil;
//    
//     [self dismissViewControllerAnimated:YES completion:nil];
//}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

- (IBAction)back:(id)sender
{
    CATransition *animation = [CATransition animation];
    animation.duration = 0.4;
//    animation.timingFunction = UIViewAnimationCurveEaseInOut;
    animation.type = kCATransitionPush;
    animation.subtype = kCATransitionFromLeft;
    [self.view.window.layer addAnimation:animation forKey:nil];
    
    [self dismissViewControllerAnimated:NO completion:nil];
}
@end
